<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/_mozilla/bluetooth/bluetooth-helpers.js"></script>
<script>
'use strict';

let matching_services = [heart_rate.uuid];
let matching_name = mock_device_name.heart_rate;
let matching_namePrefix = 'Heart';

let test_specs = [{
    filters: [{
        services: matching_services,
    }]
}, {
    filters: [{
        services: matching_services,
        name: matching_name,
    }]
}, {
    filters: [{
        services: matching_services,
        namePrefix: matching_namePrefix
    }]
}, {
    filters: [{
        name: matching_name,
    }],
    optionalServices: matching_services
}, {
    filters: [{
        name: matching_name,
        namePrefix: matching_namePrefix
    }],
    optionalServices: matching_services
}, {
    filters: [{
        namePrefix: matching_namePrefix
    }],
    optionalServices: matching_services
}, {
    filters: [{
        services: matching_services,
        name: matching_name,
        namePrefix: matching_namePrefix
    }]
}];

promise_test(() => {
    window.testRunner.setBluetoothMockDataSet(adapter_type.glucose_heart_rate);
    let promises = [];
    test_specs.forEach(args => {
        promises.push(
            window.navigator.bluetooth.requestDevice(args)
            .then(device => {
                assert_equals(device.name, matching_name);
                assert_true(device.name.startsWith(matching_namePrefix));
            })
        );
    });
    return Promise.all(promises);
}, 'Matches a filter if all present members match.');
</script>
